package com.frinder.official.akka.persistence;

import akka.event.Logging;
import akka.event.LoggingAdapter;
import akka.persistence.AbstractPersistentActor;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

/**
 * Created by frinder on 2017/10/11.
 */
@Component
@Scope("prototype")
public class MyPersistentActor2 extends AbstractPersistentActor {

    private final LoggingAdapter log = Logging.getLogger(getContext().getSystem(), this);

    @Override
    public String persistenceId() {
        return "some-persistence-id-001";
    }

    @Override
    public Receive createReceive() {
        return receiveBuilder()
                .match(Shutdown.class, shutdown -> {
                    log.info("**** {}", shutdown);
                    getContext().stop(getSelf());
                })
                .match(String.class, msg -> {
                    log.info("*** {}", msg);
                    persist("handle-" + msg, e -> log.info("***** {}", e));
                })
                .build();
    }

    @Override
    public Receive createReceiveRecover() {
        return receiveBuilder().matchAny(any -> {
            log.info("****** {}", any);
        }).build();
    }


    public static final class Shutdown {
    }

}

